import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime
import os

# 按时间戳生成日志文件名
log_filename = datetime.now().strftime("app_%Y%m%d_%H%M%S.log")

# 日志目录（可选）
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
log_path = os.path.join(log_dir, log_filename)

# 设置 RotatingFileHandler（最大 50MB，最多保留 5 个备份）
handler = RotatingFileHandler(filename=log_path, maxBytes=50 * 1024 * 1024, backupCount=5, encoding="utf-8")

# 日志格式
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)

# 设置 logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(handler)
